package antidestiny.java2dex.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.TabHost;
import android.widget.TextView;

import antidestiny.java2dex.R;
import antidestiny.utilib.Aide;

public class OpenSourceActivity extends Activity implements TabHost.OnTabChangeListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
        Aide.tryDebug(this);
        TabHost tabHost = (TabHost) findViewById(R.id.tabHost_about);
        tabHost.setup();
        tabHost.addTab(tabHost.newTabSpec("thanks").setIndicator("致谢").setContent(R.id.textview_thanks));
        tabHost.addTab(tabHost.newTabSpec("license").setIndicator("许可").setContent(R.id.scrollView_gplLicense));
        tabHost.setOnTabChangedListener(this);
        final TextView license = (TextView) findViewById(R.id.textview_license);
        license.setText("\n", TextView.BufferType.EDITABLE);
        final Thread licenseLoadThread = new Thread(new Runnable() {
            @Override
            public String toString() {
                return "license load thread";
            }
            @Override
            public void run() {
                final String string = getString(R.string.liscense);
                int l = string.length(), step = 80;
                for (int i = 0; i < l; i += step) {
                    final String toAppend = string.substring(i, (i + step) > l ? l : i + step);
                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            license.getEditableText().append(toAppend);
                            license.invalidate();
                        }
                    });
                    try {
                        synchronized (this) {
                            this.wait(200);
                        }
                    } catch (Throwable t) {
                        t.printStackTrace();
                    }
                }
            }
        });
    /**    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            license.setTextIsSelectable(true);
        }*/
        licenseLoadThread.start();
    }

    Handler handler = new MyHandler();

    static class MyHandler extends Handler {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
        }
    }

    @Override
    public void onTabChanged(String tabId) {
        switch (tabId) {
            case "license":
                break;
        }
    }
}
